package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.CommonError;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ybl
 * @date 2023/3/7 14:54
 * @description 教师-课程信息编辑接口 实现类
 * @since version-1.0
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired(required = false)
    private CourseTeacherMapper courseTeacherMapper;

    /**
     * @author ybl
     * @date 2023/3/7 14:53
     * @description 课程教师查询接口
     * @param id courseId
     * @return java.util.List<com.xuecheng.content.model.po.CourseTeacher>
     * @since version-1.0
     */
    @Override
    public List<CourseTeacher> getCourseTeacherList(Long id) {
        // 查询此课程所有教师信息，返回
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, CourseTeacher::getCourseId, id);
        return courseTeacherMapper.selectList(queryWrapper);
    }

    /**
     * @author ybl
     * @date 2023/3/7 15:08
     * @description 课程教师新增或修改接口
     * @param courseTeacher 新增或修改的教师数据
     * @return com.xuecheng.content.model.po.CourseTeacher
     * @since version-1.0
     */
    @Transactional
    @Override
    public CourseTeacher saveCourseTeacher(CourseTeacher courseTeacher) {
        Long id = courseTeacher.getId();
        CourseTeacher selectTeacher = courseTeacherMapper.selectById(id);
        if (selectTeacher == null) {
            // 新增 初始化对象
            selectTeacher = new CourseTeacher();
            BeanUtils.copyProperties(courseTeacher, selectTeacher);
            courseTeacherMapper.insert(selectTeacher);
        } else {
            // 修改
            BeanUtils.copyProperties(courseTeacher, selectTeacher);
            courseTeacherMapper.updateById(selectTeacher);
        }
        return courseTeacherMapper.selectById(selectTeacher);
    }

    /**
     * @author ybl
     * @date 2023/3/7 15:56
     * @description 课程教师删除接口
     * @param courseId 课程id
     * @param id 教师id
     * @since version-1.0
     */
    @Transactional
    @Override
    public void deleteCourseTeacher(Long courseId, Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(courseId != null, CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);
        for (int i = 0; i < courseTeachers.size(); i++) {
            CourseTeacher courseTeacher = courseTeachers.get(i);
            if (courseTeacher.getId().equals(id)) {
                courseTeacherMapper.deleteById(id);
                return;
            }
            if ((i + 1) == courseTeachers.size()) {
                XueChengPlusException.cast(CommonError.PARAMS_ERROR);
            }
        }
    }
}
